package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.OfflineMapCityBean;
import cn.car91.bean.OfflineMapCityBean.Flag;

public class MyOfflineCityBeanAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;

    /**
     * 离线地图的数据
     */
    private List<OfflineMapCityBean> mDatas = new ArrayList<OfflineMapCityBean>();

    public List<OfflineMapCityBean> getmDatas() {
        return mDatas;
    }

    public void setmDatas(List<OfflineMapCityBean> mDatas) {
        this.mDatas = mDatas;
    }

    public MyOfflineCityBeanAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public boolean isEnabled(int position) {
        if (mDatas.get(position).getProgress() == 100) {
            return false;
        }
        return super.isEnabled(position);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OfflineMapCityBean bean = mDatas.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.view_offlinemap_item,
                    parent, false);
            holder.cityName = (TextView) convertView
                    .findViewById(R.id.cityname);
            holder.progress = (TextView) convertView
                    .findViewById(R.id.txtprogress);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.cityName.setText(bean.getCityName());
        int progress = bean.getProgress();
        String progressMsg = "";
        // 根据进度情况，设置显示
        if (progress == 0) {
            progressMsg = "未下载";
        } else if (progress == 100) {
            bean.setFlag(Flag.NO_STATUS);
            progressMsg = "已下载";
        } else {
            progressMsg = progress + "%";
        }
        // 根据当前状态，设置显示
        switch (bean.getFlag()) {
            case PAUSE:
                progressMsg += "【等待下载】";
                break;
            case DOWNLOADING:
                progressMsg += "【正在下载】";
                break;
            case UPDATE:
                progressMsg = "【有更新】";
                break;
            default:
                break;
        }
        holder.progress.setText(progressMsg);

        return convertView;
    }

    private class ViewHolder {
        TextView cityName;
        TextView progress;
    }
}
